package com.kaka.bus.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.kaka.bus.R;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: document your custom view class.
 */
public class AuthenticationView extends View {
    private String textString; // TODO: use a default from R.string...
    private int textColor = Color.RED; // TODO: use a default from R.color...
    private float textDimension = 0; // TODO: use a default from R.dimen...
    private float mRoundDimension = 10; // TODO: use a default from R.dimen...
    //    private Drawable mExampleDrawable;
    private List<String> strings;
    private TextPaint mTextPaint;
    private float mTextWidth;
    private Paint mLinePaint;
    private float mTextHeight;
    private Paint mRoundPaint;
    private Paint mRoundminPaint;

    private int mType = -1;
    private float textPadding;
    private int maxRound = 40;
    private int minRound = 26;
    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect mBound;
    private Paint mdfLinePaint;
    private Paint mdfRoundPaint;
    private Paint mdfRoundminPaint;
//    class AuthBean {
//        private List<String> strs;
//    }

    public AuthenticationView(Context context) {
        super(context);
        init(null, 0);
    }

    public AuthenticationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public AuthenticationView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    public void updateView(int type) {
        mType = type;
        invalidate();
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        strings = new ArrayList<>();
        strings.add("身份认证");
        strings.add("个人认证");
        strings.add("人脸识别");
        strings.add("手机认证");
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.AuthenticationView, defStyle, 0);

        textString = a.getString(
                R.styleable.AuthenticationView_textString);
        textColor = a.getColor(
                R.styleable.AuthenticationView_textColor,
                textColor);
        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
        // values that should fall on pixel boundaries.
        textDimension = a.getDimension(
                R.styleable.AuthenticationView_textDimension,
                textDimension);
        mRoundDimension = a.getDimension(
                R.styleable.AuthenticationView_roundDimension,
                mRoundDimension);
        textPadding = a.getDimension(
                R.styleable.AuthenticationView_textPadding,
                textPadding);


        a.recycle();
        mTextPaint = new TextPaint();
        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        mBound = new Rect();
        mTextPaint.getTextBounds(textString, 0, textString.length(), mBound);
        // Set up a default TextPaint object


        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setDither(true);
        mLinePaint.setColor(getResources().getColor(R.color.ff8447));
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(6);

        mdfLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mdfLinePaint.setDither(true);
        mdfLinePaint.setColor((Color.rgb(192, 192, 192)));
        mdfLinePaint.setStyle(Paint.Style.STROKE);
        mdfLinePaint.setStrokeWidth(6);

        // Update TextPaint and text measurements from attributes
        invalidateTextPaintAndMeasurements();
    }

    private void invalidateTextPaintAndMeasurements() {
        mTextPaint.setTextSize(textDimension);
        mTextPaint.setColor(textColor);
        mTextWidth = mTextPaint.measureText(textString);


        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mTextHeight = fontMetrics.bottom;
        //        实例化画笔对象
        mRoundPaint = new Paint();
        mRoundminPaint = new Paint();

        mdfRoundPaint = new Paint();
        mdfRoundminPaint = new Paint();
//        给画笔设置颜色
        mRoundPaint.setColor(getResources().getColor(R.color.ffe0d4));
        mRoundminPaint.setColor(getResources().getColor(R.color.ff8447));
        mdfRoundPaint.setColor(Color.rgb(245, 245, 245));
        mdfRoundminPaint.setColor(Color.rgb(220, 220, 220));
//        设置画笔属性
        mRoundPaint.setStyle(Paint.Style.FILL);
        mRoundminPaint.setStyle(Paint.Style.FILL);//画笔属性是实心圆
        mdfRoundPaint.setStyle(Paint.Style.FILL);//画笔属性是实心圆
        mdfRoundminPaint.setStyle(Paint.Style.FILL);//画笔属性是实心圆
//        mRoundPaint.setStyle(Paint.Style.STROKE);//画笔属性是空心圆
//        mRoundPaint.setStrokeWidth(8);//设置画笔粗细
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // TODO: consider storing these as member variables to reduce
        // allocations per draw cycle.
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        int contentWidth = getWidth() - paddingLeft - paddingRight;
        int contentHeight = getHeight() - paddingTop - paddingBottom;
        int size = strings.size();
        for (int i = 0; i < size; i++) {
            int lineWidth = (contentWidth - size * maxRound) / (size - 1);
            float val = maxRound * i + lineWidth * i;
            if (i < mType || i == mType) {
                canvas.drawLine(val + maxRound + 5, getExampleDimension() + textPadding + maxRound / 2, val + maxRound + lineWidth - 5, getExampleDimension() + textPadding + maxRound / 2, mLinePaint);
                canvas.drawCircle(val + maxRound / 2, getExampleDimension() + textPadding + maxRound / 2, maxRound / 2, mRoundPaint);
                canvas.drawCircle(val + maxRound / 2, getExampleDimension() + textPadding + maxRound / 2, minRound / 2, mRoundminPaint);

            } else {
                canvas.drawLine(val + maxRound + 5, getExampleDimension() + textPadding + maxRound / 2, val + maxRound + lineWidth - 5, getExampleDimension() + textPadding + maxRound / 2, mdfLinePaint);
                canvas.drawCircle(val + maxRound / 2, getExampleDimension() + textPadding + maxRound / 2, maxRound / 2, mdfRoundPaint);
                canvas.drawCircle(val + maxRound / 2, getExampleDimension() + textPadding + maxRound / 2, minRound / 2, mdfRoundminPaint);

            }

            if (i == 0) {
                canvas.drawText(strings.get(i), 0, contentHeight / 3, mTextPaint);

            } else if (i == 3) {
                canvas.drawText(strings.get(i), val - mTextWidth + maxRound, contentHeight / 3, mTextPaint);

            } else {
                canvas.drawText(strings.get(i), val - mTextWidth / 2 + maxRound / 2, contentHeight / 3, mTextPaint);
            }
        }


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
//                mTextPaint.setTextSize(mTitleTextSize);
//                mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);
//                float textWidth = mBounds.width();
//                int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width = getMeasuredHeight();
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            mTextPaint.setTextSize(textDimension);
            mTextPaint.getTextBounds(textString, 0, textString.length(), mBound);
            float textHeight = mBound.height();
            int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom() + textPadding + maxRound);
            height = desired;
        }
        setMeasuredDimension(width, height);
    }

    /**
     * Gets the example string attribute value.
     *
     * @return The example string attribute value.
     */
    public String getExampleString() {
        return textString;
    }

    /**
     * Sets the view's example string attribute value. In the example view, this string
     * is the text to draw.
     *
     * @param exampleString The example string attribute value to use.
     */
    public void setExampleString(String exampleString) {
        textString = exampleString;
        invalidateTextPaintAndMeasurements();
    }

    /**
     * Gets the example color attribute value.
     *
     * @return The example color attribute value.
     */
    public int getExampleColor() {
        return textColor;
    }

    /**
     * Sets the view's example color attribute value. In the example view, this color
     * is the font color.
     *
     * @param exampleColor The example color attribute value to use.
     */
    public void setExampleColor(int exampleColor) {
        textColor = exampleColor;
        invalidateTextPaintAndMeasurements();
    }

    /**
     * Gets the example dimension attribute value.
     *
     * @return The example dimension attribute value.
     */
    public float getExampleDimension() {
        return textDimension;
    }

    /**
     * Sets the view's example dimension attribute value. In the example view, this dimension
     * is the font size.
     *
     * @param exampleDimension The example dimension attribute value to use.
     */
    public void setExampleDimension(float exampleDimension) {
        textDimension = exampleDimension;
        invalidateTextPaintAndMeasurements();
    }

    /**
     * Gets the example drawable attribute value.
     *
     * @return The example drawable attribute value.
     */
//    public Drawable getExampleDrawable() {
//        return mExampleDrawable;
//    }

    /**
     * Sets the view's example drawable attribute value. In the example view, this drawable is
     * drawn above the text.
     *
     * @param exampleDrawable The example drawable attribute value to use.
     */
//    public void setExampleDrawable(Drawable exampleDrawable) {
//        mExampleDrawable = exampleDrawable;
//    }
}
